package com.byl.modules.sys.controller;

import com.byl.common.modules.controller.BaseController;
import com.byl.modules.sys.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/8/20.
 */
@RestController
@RequestMapping("/api/template")
public class TemplateController extends BaseController {

    @Autowired
    TemplateService templateService;

    @GetMapping("/list")
    public String listTemplate(){
        List list=templateService.listTemplate();
        return success("模板列表",list);
    }

    @PostMapping("/save")
    public String saveTemplate(@RequestBody Map map){
        int rs=templateService.saveTemplate(map);
        if(rs>0)
        return success("保存成功","");
        return fail("保存失败");
    }

    @GetMapping("/delete")
    public String deleteTemplate(String id){
        int rs=templateService.deleteTemplate(id);
        if(rs>0){
            return success("删除成功","");
        }else if(rs==-1){
            return fail("该模板存在业务映射，无法删除！");
        }
        return fail("删除失败");
    }

    @GetMapping("/check")
    public String checkTemplate(String template_name){
        int rs=templateService.checkTemplate(template_name);
        return success("检查模板是否存在",rs);
    }

    @GetMapping("/field/list")
    public String listField(String tid){
        List list=templateService.listField(tid);
        return success("模板字段列表",list);
    }

    @PostMapping("/field/save")
    public String saveField(@RequestBody Map map){
        int rs=templateService.saveField(map);
        if(rs>0)
        return success("保存成功","");
        return fail("保存失败");
    }

    @PostMapping("/field/update")
    public String updateField(@RequestBody Map map){
        int rs=templateService.updateField(map);
        if(rs>0)
        return success("修改成功","");
        return fail("修改失败");
    }
    @GetMapping("/field/delete")
    public String deleteField(String id){
        int rs=templateService.deleteField(id);
        if(rs>0)
        return success("删除成功","");
        return fail("删除失败");
    }

    @GetMapping("/field/option/list")
    public String listOption(String fid){
        List list=templateService.listOption(fid);
        return success("字段选项列表",list);
    }

    @PostMapping("/field/option/save")
    public String saveOption(@RequestBody Map map){
        int rs=templateService.saveOption(map);
        if(rs>0)
        return success("保存成功","");
        return fail("保存失败");
    }

    @PostMapping("/field/option/update")
    public String updateOption(@RequestBody Map map){
        int rs=templateService.updateOption(map);
        if(rs>0)
            return success("修改成功","");
        return fail("修改失败");
    }

    @GetMapping("/field/option/delete")
    public String deleteOption(String id){
        int rs=templateService.deleteOption(id);
        if(rs>0)
            return success("刪除成功","");
        return fail("刪除失败");
    }
}
